﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;

/// <summary>
/// Summary description for CAdmin
/// </summary>
public class CAdmin
{
	public CAdmin()
	{
		//
		// TODO: Add constructor logic here
		//
	}

    public bool AddUser(string name, string pwd, string role)
    {
        DataAccess d = new DataAccess();
        d.Open();
        Dictionary<object, object> pars = new Dictionary<object, object>();
        pars.Add("Name", name);
        pars.Add("Pwd", pwd);
        pars.Add("Role", role);
        if (d.ExeNonQuery("insert into admin(username,userpwd,userrole) values(@Name,@Pwd,@Role)", pars) > 0)
        {
            d.Close();
            return true;
        }
        else
        {
            d.Close();
            return false;
        }
    }

    public bool ResetUserInfo(int id, string name, string pwd, string role)
    {
        DataAccess d = new DataAccess();
        d.Open();
        Dictionary<object, object> pars = new Dictionary<object, object>();
        pars.Add("Name", name);
        pars.Add("Pwd", pwd);
        pars.Add("Role", role);
        pars.Add("ID", id);
        if (d.ExeNonQuery("update admin set username=@Name,userpwd=@Pwd,userrole=@Role where id=@ID", pars) > 0)
        {
            d.Close();
            return true;
        }
        else
        {
            d.Close();
            return false;
        }
    }

    public bool CheckUserName(string name)
    {
        DataAccess d = new DataAccess();
        d.Open();
        Dictionary<object, object> pars = new Dictionary<object, object>();
        pars.Add("Name", name);
        if (d.ExeScalar("select count(*) from admin where username=@Name",pars).ToString() != "0")
        {
            d.Close();
            return true;
        }
        d.Close();
        return false;
    }

    public bool DeleteUser(int id)
    {
        DataAccess d = new DataAccess();
        d.Open();
        Dictionary<object, object> pars = new Dictionary<object, object>();
        pars.Add("ID", id);
        if (d.ExeNonQuery("delete from admin where id=@ID", pars) > 0)
        {
            d.Close();
            return true;
        }
        else
        {
            d.Close();
            return false;
        }
    }

    public string Login(string name, string pwd)
    {
        DataAccess d = new DataAccess();
        d.Open();
        Dictionary<object, object> pars = new Dictionary<object, object>();
        pars.Add("Name", name);
        pars.Add("Pwd", pwd);
        DataTable dt = d.ExeDataTable("select * from admin where username=@Name and userpwd=@Pwd", pars);
        d.Close();
        if (null != dt)
        {
            return dt.Rows[0]["id"].ToString();
        }
        else
        {
            return null;
        }
    }

    public DataTable ReadUsers()
    {
        DataAccess d = new DataAccess();
        d.Open();
        DataTable dt = d.ExeDataTable("select * from admin", null);
        d.Close();
        return dt;
    }

    public DataRow ReadUser(int id)
    {
        DataAccess d = new DataAccess();
        d.Open();
        Dictionary<object, object> pars = new Dictionary<object, object>();
        pars.Add("ID", id);
        DataTable dt = d.ExeDataTable("select * from admin where id=@ID", pars);
        d.Close();
        if (null != dt)
        {
            return dt.Rows[0];
        }
        return null;
    }
}